export function countNumberFrequency(tickets) {
  const frontCounts = {}
  const backCounts = {}
  const lines = tickets?.trim()?.split('\n') || []

  lines.forEach((line) => {
    // 去除行内可能的空格
    const cleanLine = line.replace(/\s/g, '').replace(',,', ',')

    const [frontPart = '', backPart = ''] = cleanLine.split(',')
    // 前半部分两两拆分（纯字符串）
    for (let i = 0; i + 1 < frontPart.length; i += 2) {
      const numStr = frontPart.substring(i, i + 2)
      frontCounts[numStr] = (frontCounts[numStr] || 0) + 1
    }

    // 后半部分两两拆分（纯字符串）
    for (let i = 0; i + 1 < backPart.length; i += 2) {
      const numStr = backPart.substring(i, i + 2)
      backCounts[numStr] = (backCounts[numStr] || 0) + 1
    }
  })

  const formatResult = (countsObj) => {
    return Object.entries(countsObj)
      .map(([num, count]) => ({ count, num })) // 保持num为字符串
      .sort((a, b) => b.count - a.count)
  }

  return {
    front: formatResult(frontCounts),
    back: formatResult(backCounts),
  }
}
